/**
 * 
 */
var chart = {
	showHighChart: function(param){
		var type = param.chartType || "bar";		// 图表类型
		var callback = param.success || function(){};
		var options = {};
		
		// 显示横向多柱图或折线图
		if(type == "bar" || type == "line" || type == "column"){
			if(type == "bar"){
				options = {
		            bar: {
		                dataLabels: {
		                    enabled: true
		                }
		            }
		        }
			} else if(type == "column"){
				options = {
					column: {
	                    pointPadding: 0.2,
	                    borderWidth: 0
	                }
				}
			}
			
			param.options = options;
			oap.showBar(param);
		}
		
		// 显示饼图
		if(type == "pie"){
			oap.showPie(param);
		}
		
		callback.call();
	},
	
	/**
	 * 柱图或折线图
	 */
	showBar: function(param){
		var type = param.chartType || "bar";		// 图表类型
		var chartDiv = param.chartDiv;				// 显示图表div的id
		var data = param.data;						// 图表数据
		var subtitle = param.subtitle || "";		// 图表子标题
		var options = param.options;
		var extendXAxis = param.extendXAxis;		// 扩展横坐标标题
		extendXAxis = extendXAxis == undefined ? false : true;
		
		var title = data.chart.caption || "";		// 图表标题
		var xAxisName = data.chart.xAxisName || ""; // 图表横坐标标题
		var yAxisName = data.chart.yAxisName || ""; // 图表纵坐标标题
		
		// 纵坐标值
		var categories = new Array();
		var categoryArr = new Array();
		if(data.categories){
			categories = data.categories[0].category;
			for(var i in categories) {
				categoryArr[i] = categories[i].label;
			}
		} else if(data.data) {
			for(var i in data.data){
				if(extendXAxis){
					categoryArr[i] = data.data[i].label + "/" + data.data[i].extendLabel;
				} else {
					categoryArr[i] = data.data[i].label;
				}
			}
		}
		
		
		// 显示数据
		var dataset = data.dataset;
		var seriesArr = new Array();
		if(dataset){
			
			for(var i in dataset){
				var content = {};
				// 数据列标题
				var seriesName = dataset[i].seriesName || "";
				content.name = seriesName;
				
				// 显示颜色
				var seriesColor = dataset[i].color;
				content.color = "#" + seriesColor;
				
				// 数据值
				var seriesData = dataset[i].data;
				var newData = new Array();
				for(var j in seriesData){
					if(seriesData[j].value.indexOf(".") > 0)
						newData[j] = parseFloat(seriesData[j].value);
					else
						newData[j] = parseInt(seriesData[j].value);
				}
				content.data = newData;
				
				seriesArr[i] = content;
			}
		} else if(data.data){
			dataset = data.data;
			
			var content = {};
			var newData = new Array();
			for(var i in dataset){
				var dataPar = {};
				var dataVal = dataset[i].value + "";
				if(dataVal.indexOf(".") > 0)
					dataPar.y = parseFloat(dataset[i].value);
				else
					dataPar.y = parseInt(dataset[i].value);
				dataPar.color = "#" + dataset[i].color;
				newData[i] = dataPar;
			}
			content.data = newData;
			content.name = yAxisName;
			seriesArr[0] = content;
		}
		
	    $('#' + chartDiv).highcharts({
	        chart: {
	            type: type
	        },
	        title: {
	            text: title
	        },
	        subtitle: {
	            text: subtitle
	        },
	        xAxis: {
	            categories: categoryArr
	        },
	        yAxis: {
	            min: 0,
	            title: {
	                text: yAxisName,
	                align: 'high'
	            },
	            labels: {
	                overflow: 'justify'
	            }
	        },
	        tooltip: {
	            valueSuffix: ''
	        },
	        plotOptions: options,
	        legend: {
	            layout: 'vertical',
	            align: 'right',
	            verticalAlign: 'top',
	            x: -40,
	            y: 100,
	            floating: true,
	            borderWidth: 1,
	            backgroundColor: '#FFFFFF',
	            shadow: true
	        },
	        credits: {
	            enabled: false
	        },
	        series: seriesArr
	    });
	},
	
	/**
	 * 饼图
	 */
	showPie: function(param){
		var type = param.chartType || "bar";		// 图表类型
		var chartDiv = param.chartDiv;				// 显示图表div的id
		var data = param.data;						// 图表数据
		var pointName = param.pointName || "";		// 图表子标题
		
		var title = data.chart.caption || "";		// 图表标题
		
		var dataset = data.data;
		var dataArr = new Array();
		for(var i in dataset){
			var content = {};
			
			content.name = dataset[i].label || dataset[i].series;
			if(dataset[i].value.indexOf(".") > 0)
				content.y = parseFloat(dataset[i].value);
			else
				content.y = parseInt(dataset[i].value);
			
			content.color = "#" + dataset[i].color;
			
			dataArr[i] = content;
		}
		
		$('#' + chartDiv).highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            plotShadow: false
	        },
	        title: {
	            text: title
	        },
	        tooltip: {
	    	    pointFormat: '<b>{point.percentage:.2f}%</b>'
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    color: '#000000',
	                    connectorColor: '#000000',
	                    format: '<b>{point.name}</b>: {point.y}<br/><b>占比</b>:{point.percentage:.2f} %'
	                },
	        		showInLegend: true
	            }
	        },
	        credits: {
	            enabled: false
	        },
	        series: [{
	            type: type,
	            data: dataArr
	        }]
	    });
	}
}